package com.apotik.legenda.dialog;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import java.awt.Color;

import javax.swing.JButton;

import com.apotik.legenda.model.SalesTrxObat;
import com.apotik.legenda.observer.PosObserver;
import com.apotik.legenda.panel.PosPanel;
import com.apotik.legenda.util.AppUtil;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.math.BigDecimal;
import java.util.HashMap;

public class EditTrxObatQuantityDialog extends JDialog implements ActionListener, KeyListener {
	private static final long serialVersionUID = 1L;

	private PosObserver observer;
	private int rowIndex;
	private SalesTrxObat salesTrxObat;
	
	private JPanel panel;
	private JTextField textFieldName;
	private JTextField textFieldQuantity;
	private JTextField textFieldPrice;
	private JTextField textFieldTotal;
	private JLabel lblTotal;
	private JButton btnSubmit;
	private JButton btnCancel;
	
	public EditTrxObatQuantityDialog(PosObserver observer, int rowIndex, SalesTrxObat salesTrxObat) {
		this.observer = observer;
		this.rowIndex = rowIndex;
		this.salesTrxObat = salesTrxObat;
		
		setTitle("Edit Quantity");
		setSize(379, 207);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		addKeyListener(this);
		
		panel = new JPanel();
		panel.setLayout(null);
		getContentPane().add(panel);
		
		JLabel lblName = new JLabel("Nama Barang");
		lblName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblName.setBounds(15, 15, 90, 20);
		panel.add(lblName);
		
		textFieldName = new JTextField();
		textFieldName.setFont(new Font("Tahoma", Font.PLAIN, 12));
		textFieldName.setBounds(140, 13, 200, 22);
		textFieldName.setColumns(10);
		textFieldName.setEnabled(false);
		textFieldName.setText(salesTrxObat.getProductName());
		panel.add(textFieldName);
		
		JLabel lblPrice = new JLabel("Harga Satuan (Rp)");
		lblPrice.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPrice.setBounds(15, 43, 119, 20);
		panel.add(lblPrice);
		
		textFieldPrice = new JTextField();
		textFieldPrice.setEnabled(false);
		textFieldPrice.setColumns(10);
		textFieldPrice.setFont(new Font("Tahoma", Font.PLAIN, 12));
		textFieldPrice.setBounds(140, 41, 200, 22);
		//textFieldPrice.setAlignmentX(RIGHT_ALIGNMENT);
		textFieldPrice.setText(AppUtil.formatStringIDR(salesTrxObat.getAmountUnitPrice().intValue()));
		panel.add(textFieldPrice);
		
		JLabel lblQuantity = new JLabel("Quantity");
		lblQuantity.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblQuantity.setBounds(15, 71, 90, 20);
		panel.add(lblQuantity);
		
		textFieldQuantity = new JTextField();
		textFieldQuantity.setColumns(10);
		textFieldQuantity.setFont(new Font("Tahoma", Font.PLAIN, 12));
		textFieldQuantity.setBounds(140, 69, 200, 22);
		textFieldQuantity.setText(salesTrxObat.getQuantity().toString());
		textFieldQuantity.addKeyListener(this);
		panel.add(textFieldQuantity);
		
		lblTotal = new JLabel("Total (Rp)");
		lblTotal.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblTotal.setBounds(15, 98, 90, 20);
		panel.add(lblTotal);
		
		textFieldTotal = new JTextField();
		textFieldTotal.setEnabled(false);
		textFieldTotal.setFont(new Font("Tahoma", Font.PLAIN, 12));
		textFieldTotal.setColumns(10);
		textFieldTotal.setBounds(140, 97, 200, 22);
		textFieldTotal.setText(AppUtil.formatStringIDR(salesTrxObat.getAmountTrxObat().intValue()));
		panel.add(textFieldTotal);
		
		btnSubmit = new JButton("Update");
		btnSubmit.setBounds(139, 126, 80, 25);
		btnSubmit.addActionListener(this);
		btnSubmit.addKeyListener(this);
		this.getRootPane().setDefaultButton(btnSubmit);
		panel.add(btnSubmit);
		
		btnCancel = new JButton("Batal");
		btnCancel.setBounds(224, 126, 80, 25);
		btnCancel.addActionListener(this);
		btnCancel.addKeyListener(this);
		panel.add(btnCancel);
		
		JPanel boxPanel = new JPanel();
		boxPanel.setBorder(new LineBorder(new Color(169, 169, 169)));
		boxPanel.setBounds(5, 4, 350, 158);
		panel.add(boxPanel);
	}
	
	private void btnSubmitAction(){
		String quantityStr = textFieldQuantity.getText();
		if(quantityStr.trim().isEmpty()){
			JOptionPane.showMessageDialog(null, "Quantity harus lebih dari 0", "Error", JOptionPane.ERROR_MESSAGE);
		}else{
			int rowIndex = this.rowIndex;
			SalesTrxObat sto = this.salesTrxObat;
			int quantity = Integer.parseInt(quantityStr);
			
			HashMap<String, Object> hm = new HashMap<>();
			hm.put("update_for", PosPanel.UPDATE_QUANTITY);
			hm.put("rowIndex", rowIndex);
			hm.put("salesTrxObat", sto);
			hm.put("quantity", quantity);
			
			observer.updateProduct(hm);
			
			this.setVisible(false);
		}
	}
	
	
	
	public void actionPerformed(ActionEvent ae) {
		Object obj = ae.getSource();
		
		if(obj==btnCancel){
			this.setVisible(false);
		}else if(obj==btnSubmit){
			btnSubmitAction();
		}
	}

	@Override
	public void keyPressed(KeyEvent ke) {
		if(ke.getKeyCode()==KeyEvent.VK_ESCAPE){
			this.setVisible(false);
		}
		
		Object source = ke.getSource();
		if(source==btnSubmit){
			if(ke.getKeyCode()==KeyEvent.VK_ENTER){
				btnSubmitAction();
			}
		}else if(source==btnCancel){
			if(ke.getKeyCode()==KeyEvent.VK_ENTER){
				this.setVisible(false);
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent ke) {
		Object obj = ke.getSource();
		if(obj==textFieldQuantity){
			if(ke.getKeyCode()==KeyEvent.VK_1 || ke.getKeyCode()==KeyEvent.VK_2 || ke.getKeyCode()==KeyEvent.VK_3 || 
					ke.getKeyCode()==KeyEvent.VK_4 || ke.getKeyCode()==KeyEvent.VK_5 || ke.getKeyCode()==KeyEvent.VK_6 || 
					ke.getKeyCode()==KeyEvent.VK_7 || ke.getKeyCode()==KeyEvent.VK_8 || ke.getKeyCode()==KeyEvent.VK_9 ){
				
				int quantity = Integer.parseInt(textFieldQuantity.getText().trim());
				BigDecimal hargaSatuan = this.salesTrxObat.getAmountUnitPrice();
				BigDecimal sumHarga = hargaSatuan.multiply(new BigDecimal(quantity));
				
				textFieldTotal.setText(AppUtil.formatStringIDR(sumHarga.intValue()));
			}
		}
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
	}
	
}
